package com.gong.datareceive.controller;

import com.alibaba.druid.support.json.JSONUtils;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.gong.datareceive.pojo.Data;
import com.gong.datareceive.pojo.bo.DataBo;
import com.gong.datareceive.service.DataService;
import com.rabbitmq.tools.json.JSONUtil;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.SocketException;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * @Description
 * @Author gongchang
 * @Date 2021-08-15 14:23
 */
@Component
public class Main {
    @Autowired
    AmqpTemplate amqpTemplate;
    @Autowired
    DataService dataService;


    public void recive(){
        DatagramSocket socket = null;
        try {
            byte [] message = new byte[2048];
            socket = new DatagramSocket(8888);
            DatagramPacket packet = new DatagramPacket(message, message.length);
            while (true){
                //接受数据
                socket.receive(packet);
                byte[] data = packet.getData();
                String str = new String(data);
                //将数据转为对象
                Map<String, String> map = mapStringToMap(str);
                Data data1 = getData(map);
                System.out.println(data1);
                //发送到消息队列
                amqpTemplate.convertAndSend("DataDirectExchange","DataDirectRouting",data1.getGrandid()+" "+data1.getEquid());
                //存入数据库
                dataService.save(data1);
            }
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            socket.close();
        }
    }

    private static Data getData(Map<String, String> map) {
        Data data= new Data();
        data.setGrandid(Integer.parseInt(map.get("grandid")));
        data.setEquid(Integer.parseInt(map.get("equid")));
        data.setDigital(Double.parseDouble(map.get("digital")));
        data.setTime(new Date(map.get("time")));

        return data;
    }

    public static Map<String,String> mapStringToMap(String str){
        str=str.substring(1, str.length()-1);
        str = str.split("}")[0];
        String[] strs=str.split(", ");
        Map<String,String> map = new HashMap<String, String>();
        for (String string : strs) {
            String key=string.split("=")[0];
            String value=string.split("=")[1];
            map.put(key, value);
        }
        return map;
    }
}
